package com.beihua;

import com.beihua.dao.UserDao;
import com.beihua.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class UserTest1 {
    @Test
    public void testSelectList() {
//1.加载 mybatis 的核心配置文件 a，获取 SqlSessionFactory 对象
        SqlSession sqlSession = null;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
//可以用 Reader
//Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2.获取 SqlSession 对象，执行 sql 语句
            sqlSession = sqlSessionFactory.openSession();
// 3.执行 sql 语句
            List<User> users = sqlSession.selectList("getUserList");
//            List<User> users = sqlSession.selectList("getUserList2", new User(null, "张", null, null));
            System.out.println(users);
            for (int i = 0; i < users.size(); i++) {
                System.out.println(users.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 4.释放资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void addUserTest() {
//1.加载 mybatis 的核心配置文件 a，获取 SqlSessionFactory 对象
        SqlSession sqlSession = null;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取 SqlSession 对象，执行 sql 语句
            sqlSession = sqlSessionFactory.openSession();
//3.执行 sql 语句
/*User user = new User(6,"张飞 1","男",21);
sqlSession.insert("addUser",user);*/
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.addUser(new User(7, "lisi", "Nv", 21));
            sqlSession.commit();
//testSelectList(); //查看插入数据情况
            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 4.释放资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }


    @Test
    public void updateUserTest() {
//1.加载 mybatis 的核心配置文件 a，获取 SqlSessionFactory 对象
        SqlSession sqlSession = null;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取 SqlSession 对象，执行 sql 语句
            sqlSession = sqlSessionFactory.openSession();
//3.执行 sql 语句 id 为 6 的用户名”张飞 11“,性别为"male",22
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.updateUser(new User(6, "张飞 11", "nv", 22));
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 4.释放资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void deleteUserTest() {
//1.加载 mybatis 的核心配置文件 a，获取 SqlSessionFactory 对象
        SqlSession sqlSession = null;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取 SqlSession 对象，执行 sql 语句
            sqlSession = sqlSessionFactory.openSession();
//3.执行 sql 语句 删除 id 为 6 的用户
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.deleteUser(6);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 4.释放资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

}